<?php
// +----------------------------------------------------------------------
// | Description: 合同
// +----------------------------------------------------------------------
// | Author:  Michael_xu | gengxiaoxu@5kcrm.com
// +----------------------------------------------------------------------
namespace app\crm\model;

use app\admin\model\Record;
use app\admin\model\User;
use think\Db;
use app\admin\model\Common;
use think\Request;
use think\Validate;

class Contract extends Common
{
    /**
     * 为了数据库的整洁，同时又不影响Model和Controller的名称
     * 我们约定每个模块的数据表都加上相同的前缀，比如CRM模块用crm作为数据表前缀
     */
    protected $name = 'crm_contract';
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $autoWriteTimestamp = true;
    private $statusArr = ['0' => '待审核', '1' => '审核中', '2' => '审核通过', '3' => '已拒绝', '4' => '已撤回'];
    private $stateArr = ['1' => '待捡取', '2' => '待分配', '3' => '待接收', '4' => '执行中', '5' => '待验收'];

    /**
     * [getDataList 合同list]
     * @param     [string]                   $map [查询条件]
     * @param     [number]                   $page     [当前页数]
     * @param     [number]                   $limit    [每页数量]
     * @return    [array]                    [description]
     * @author Michael_xu
     */
    public function getDataList($request, $type = 0,$userInfo=[])
    {
        $userModel = new \app\admin\model\User();
        $structureModel = new \app\admin\model\Structure();
        $fieldModel = new \app\admin\model\Field();
        $receivablesModel = new \app\crm\model\Receivables();
        $search = $request['search'];
        $user_id = $request['user_id'];
        $scene_id = (int)$request['scene_id'];
        $owner_user_id_my=isset($request["owner_user_id_my"])?$request["owner_user_id_my"]:"";
        unset($request['owner_user_id_my']);
        unset($request['scene_id']);
        unset($request['search']);
        unset($request['user_id']);
        //修改 start
        $where = [];
        if (isset($request["is_ok"])) {
            $where["is_ok"] = $request["is_ok"];
            unset($request['is_ok']);
        }
        // if (isset($request["check_status"])) {
        //     $where["check_status"] = $request["check_status"];
        //     unset($request['check_status']);
        // }
        /*if(isset($request["owner_user_id"])){
            //判断是否是管家身份
            $userInfo=$userModel->where(["id"=>$request["owner_user_id"]])->find();
            //判断是不是外勤和会计
            $idArr=db('crm_contract')->where('ro_user_id',array('like','%,'.$userInfo['id'].',%'))
                ->whereOr('rw_user_id',array('like','%,'.$userInfo['id'].',%'))->field("contract_id")->select();
            if($userInfo["structure_id"]==6){
                $where["contract.butler_id"]=["in",[0,$userInfo["id"]]];
            }
            $where["contract.butler_id"]=["=",$request["owner_user_id"]];
            unset($request['owner_user_id']);
            unset($request['admin_realname']);
        }*/

        //修改 end
        $request = $this->fmtRequest($request);
        $requestMap = $request['map'] ?: [];
        $sceneModel = new \app\admin\model\Scene();
        if ($scene_id) {
            //自定义场景
            $sceneMap = $sceneModel->getDataById($scene_id, $user_id, 'contract') ?: [];
        } else {
            //默认场景
            $sceneMap = $sceneModel->getDefaultData('contract', $user_id) ?: [];
        }
        if ($search) {
            //普通筛选
            $sceneMap['name'] = ['condition' => 'contains', 'value' => $search, 'form_type' => 'text', 'name' => '合同名称'];
        }
        $partMap = [];
        //优先级：普通筛选>高级筛选>场景
        if ($sceneMap['contract.ro_user_id'] && $sceneMap['contract.rw_user_id']) {
            //相关团队查询
            $map = $requestMap;
            $partMap = function ($query) use ($sceneMap) {
                $query->where('contract.ro_user_id', array('like', '%,' . $sceneMap['ro_user_id'] . ',%'))
                    ->whereOr('contract.rw_user_id', array('like', '%,' . $sceneMap['rw_user_id'] . ',%'));
            };
        } else {
            $map = $requestMap ? array_merge($sceneMap, $requestMap) : $sceneMap;
        }
        $group_id = db('admin_access')->where(['user_id' => $user_id, "group_id" => 1])->find();
        $butler_where="";
        if (empty($group_id)) {
            if (!empty($user_id)) {
                if ($type == 1 || empty($type)) {
                    //工单库
                    //查找下级订单
                    $group_ids = db('admin_access')->where(['user_id' => $user_id])->column('group_id');
                    if (!in_array(1, $group_ids) && !in_array(32, $group_ids)) {
                        if (!empty($butler_where)) {
                            $butler_where .= " and ";
                        }
//                        $user_ids = User::field("GROUP_CONCAT(id) ids")->where(["parent_id" => $user_id])->find()["ids"];
                        $user_ids=$this->getChildIds($user_id);
                        $user_ids[]=$user_id;
                        $user_ids=implode(",",$user_ids);
                        $user_where = " (contract.owner_user_id =" . $user_id." or contract.butler_id =".$user_id." or contract.consult_user_id =".$user_id." or contract.is_accounting =".$user_id.")";
                        if (!empty($user_ids)) {
                            $user_where .= " or (contract.owner_user_id in (" . $user_ids . ") or contract.butler_id in (" . $user_ids . ") or contract.consult_user_id in (" . $user_ids . ") or contract.is_accounting in (" . $user_ids . "))";
                        }
                        $butler_where .= " (" . $user_where . ")";
                    }
                }
                //判断--工单库 管家只显示未捡取和自己捡取的数据 start 管家看到所有
                $butler_group = db('admin_access')->where(['user_id' => $user_id, "group_id" => "32"])->find();
                if (!empty($butler_group)) {
                    // $butler_where = "contract.state=1 or contract.butler_id=" . $user_id;
                    // $butler_where = " contract.state=1 ";
                }
                //判断--工单库 管家只显示未捡取和自己捡取的数据 end
                //外勤只查自己的工单
                $group = db('admin_access')->where(['user_id' => $user_id, "group_id" => 31])->find();
                if (!empty($group) && empty($butler_group)) {
                    $assignfield_contract_ids = Assignfield::field("GROUP_CONCAT(DISTINCT contract_id) ids")->where(["admin_user_id" => $user_id])->find()["ids"];
                    if($assignfield_contract_ids){
                        if(empty($butler_where)){
                            $butler_where .= "contract.contract_id in (" . $assignfield_contract_ids . ")";
                        }else{
                            $butler_where .= " or contract.contract_id in (" . $assignfield_contract_ids . ")";
                        }
                    }
                }
            }
        }
        if($owner_user_id_my){//到期订单接口专用
            $butler_where=["contract.owner_user_id"=>$owner_user_id_my];
        }
        //高级筛选
        $map = where_arr($map, 'crm', 'contract', 'index');
        $order = ['contract.update_time desc'];
        $authMap = [];
        if (!$partMap) {
            //修改 start
            if (empty($type)) {
                $auth_user_ids = $userModel->getUserByPer('crm', 'contract', 'index');
            }
            if ($type == 1) {
                $auth_user_ids = $userModel->getUserByPer('crm', 'contract', 'workindex');
            }
            if ($type == 2) {
                $auth_user_ids = $userModel->getUserByPer('crm', 'contract', 'okindex');
            }
            //修改 end
            if (isset($map['contract.owner_user_id'])) {
                if (!is_array($map['contract.owner_user_id'][1])) {
                    $map['contract.owner_user_id'][1] = [$map['contract.owner_user_id'][1]];
                }
                if ($map['contract.owner_user_id'][0] == 'neq') {
                    $auth_user_ids = array_diff($auth_user_ids, $map['contract.owner_user_id'][1]) ?: [];    //取差集
                } else {
                    $auth_user_ids = array_intersect($map['contract.owner_user_id'][1], $auth_user_ids) ?: [];    //取交集
                }
                unset($map['contract.owner_user_id']);
                $auth_user_ids = array_merge(array_unique(array_filter($auth_user_ids))) ?: ['-1'];
                $authMap['contract.owner_user_id'] = array('in', $auth_user_ids);
            } else {
                $authMapData = [];
                $authMapData['auth_user_ids'] = $auth_user_ids;
                $authMapData['user_id'] = $user_id;
                if (empty($type)) {
                    $authMap = function ($query) use ($authMapData) {
                        $query->where('contract.owner_user_id', array('in', $authMapData['auth_user_ids']))
                            ->whereOr('contract.ro_user_id', array('like', '%,' . $authMapData['user_id'] . ',%'))
                            ->whereOr('contract.rw_user_id', array('like', '%,' . $authMapData['user_id'] . ',%'));
                    };
                }
            }
        }
        //合同签约人
        if ($map['contract.order_user_id']) {
            $map['contract.order_user_id'] = ['like', '%,' . $map['contract.order_user_id'][1] . ',%'];
        }
        if($map["contract.state"]){
            $map['contract.state'][1] = array_search($map['contract.state'][1],$this->stateArr);
        }
        //列表展示字段
        // $indexField = $fieldModel->getIndexField('crm_contract', $user_id);
        //人员类型
        $userField = $fieldModel->getFieldByFormType('crm_contract', 'user');
        $structureField = $fieldModel->getFieldByFormType('crm_contract', 'structure');  //部门类型

        $readAuthIds = $userModel->getUserByPer('crm', 'contract', 'read');
        $updateAuthIds = $userModel->getUserByPer('crm', 'contract', 'update');
        $deleteAuthIds = $userModel->getUserByPer('crm', 'contract', 'delete');

        $gjWhere = '';
        if($userInfo) {
            if(isset($map['contract.check_user_id'])) {
                $group = db('admin_access')->where(['user_id' =>$userInfo['id']])->column('group_id');
                $check_user_ids = ''; 
                if(in_array(32,$group)) {
                    $gj_user = db('admin_access')->where(['group_id' => 32])->column('user_id');
                    foreach ($gj_user as $key => $value) {
                        $check_user_ids .= " contract.check_user_id like '%," . $value . ",%'";
                        // 如果不是最后一个元素，则拼接 "or"
                        if ($value !== end($gj_user)) {
                            $check_user_ids .= " or ";
                        }
                    }
                    $gjWhere = $check_user_ids;
                    unset($map['contract.check_user_id']);
                }
            }
        }
        
        $list = db('crm_contract')
            ->alias('contract')
            ->join('__CRM_CUSTOMER__ customer', 'contract.customer_id = customer.customer_id', 'LEFT')
            //->join('__CRM_BUSINESS__ business', 'contract.business_id = business.business_id', 'LEFT')
            ->join('__CRM_CONTACTS__ contacts', 'contract.contacts_id = contacts.contacts_id', 'LEFT')
            ->join('__CRM_RECEIVABLES_PLAN__ plan', 'contract.contract_id = plan.contract_id', 'LEFT')
            ->join('__ADMIN_USER__ butler', 'contract.butler_id = butler.id', 'LEFT')//管家
            ->where($map)
            ->where($partMap)
            ->where($gjWhere)
            ->where($where)
            ->where($butler_where)
//              ->where($authMap)
            ->limit(($request['page'] - 1) * $request['limit'], $request['limit'])
            ->field('contract.*,customer.name as customer_name,contacts.name as contacts_name,butler.realname butler_realname')//,business.name as business_name
            // ->field('contract_id,'.implode(',',$indexField))
            ->order($order)
            ->group('contract.contract_id')//->fetchsql()
            ->select();

        /*dump($list);
        die();*/
        $dataCount = db('crm_contract')
            ->alias('contract')
            ->join('__CRM_CUSTOMER__ customer', 'contract.customer_id = customer.customer_id', 'LEFT')
            //->join('__CRM_BUSINESS__ business', 'contract.business_id = business.business_id', 'LEFT')
            ->join('__CRM_CONTACTS__ contacts', 'contract.contacts_id = contacts.contacts_id', 'LEFT')
            ->join('__CRM_RECEIVABLES_PLAN__ plan', 'contract.contract_id = plan.contract_id', 'LEFT')
            ->join('__ADMIN_USER__ butler', 'contract.butler_id = butler.id', 'LEFT')//管家
            ->where($map)
            ->where($partMap)
            ->where($gjWhere)
            ->where($where)
            ->where($butler_where)
//                    ->where($authMap)
            ->group('contract.contract_id')->count('contract.contract_id');
        foreach ($list as $k => $v) {
            $fieldStaffArr=db('crm_assignfield')->alias("a")->join('__ADMIN_USER__ user', 'a.admin_user_id = user.id', 'LEFT')->where(["a.contract_id"=>$v["contract_id"]])->column("user.realname");//获取所有用户
            $list[$k]['handle_affairs_personal']= implode(",",$fieldStaffArr);//分配的外勤人员
            $list[$k]['create_user_id_info'] = isset($v['create_user_id']) ? $userModel->getUserById($v['create_user_id']) : [];
            $list[$k]['owner_user_id_info'] = isset($v['owner_user_id']) ? $userModel->getUserById($v['owner_user_id']) : [];
            foreach ($userField as $key => $val) {
                $list[$k][$val . '_info'] = isset($v[$val]) ? $userModel->getListByStr($v[$val]) : [];
            }
            foreach ($structureField as $key => $val) {
                $list[$k][$val . '_info'] = isset($v[$val]) ? $structureModel->getDataByStr($v[$val]) : [];
            }
           /* $list[$k]['business_id_info']['business_id'] = $v['business_id'];
            $list[$k]['business_id_info']['name'] = $v['business_name'];*/
            $list[$k]['customer_id_info']['customer_id'] = $v['customer_id'];
            $list[$k]['customer_id_info']['name'] = $v['customer_name'];
            $list[$k]['contacts_id_info']['contacts_id'] = $v['contacts_id'];
            $list[$k]['contacts_id_info']['name'] = $v['contacts_name'];
            $moneyInfo = [];
            $moneyInfo = $receivablesModel->getMoneyByContractId($v['contract_id']);
            //$list[$k]['unMoney'] = $moneyInfo['unMoney'] ? : 0.00;//未回款金额
            //$list[$k]['unMoney'] = $moneyInfo['doneMoney'] ?: 0.00;//已回款金额--修改
            $list[$k]['doneMoney'] = $moneyInfo['lasahmMoney'] ?: 0.00;//已回款金额--修改
            $list[$k]['check_status_info'] = $this->statusArr[$v['check_status']];
            //修改 lina start
            $list[$k]['state_info'] = $this->stateArr[$v['state']];
            $list[$k]['state'] = $this->stateArr[$v['state']];
            if ($v["is_ok"] == 1) {
                $list[$k]['check_status_info'] = "已完结";
                $list[$k]['state_info'] = "已完结";
            }
            if ($v["check_status"] != 2) {
                $list[$k]['state_info'] = $list[$k]['check_status_info'];
            }
            //修改 lina end
            $planInfo = [];
            $planInfo = db('crm_receivables_plan')->where(['contract_id' => $v['contract_id']])->find();
            $list[$k]['receivables_id'] = $planInfo['receivables_id'] ?: '';
            $list[$k]['remind_date'] = $planInfo['remind_date'] ?: '';
            $list[$k]['return_date'] = $planInfo['return_date'] ?: '';
            //权限
            $roPre = $userModel->rwPre($user_id, $v['ro_user_id'], $v['rw_user_id'], 'read');
            $rwPre = $userModel->rwPre($user_id, $v['ro_user_id'], $v['rw_user_id'], 'update');
            $permission = [];
            $is_read = 0;
            $is_update = 0;
            $is_delete = 0;
            if (in_array($v['owner_user_id'], $readAuthIds) || $roPre || $rwPre) $is_read = 1;
            if (in_array($v['owner_user_id'], $updateAuthIds) || $rwPre) $is_update = 1;
            if (in_array($v['owner_user_id'], $deleteAuthIds)) $is_delete = 1;
            $permission['is_read'] = $is_read;
            $permission['is_update'] = $is_update;
            $permission['is_delete'] = $is_delete;
            $list[$k]['permission'] = $permission;
            //月度服务费
            $list[$k]['crm_month_price'] = $v["monthly_agency_price"];
            $list[$k]['crm_oeseng']=$moneyInfo["hsOeseng"];
            if (!empty($list[$k]['crm_grvgkz']) && empty($v["monthly_agency_price"]) || $v["monthly_agency_price"]<=0) {
               /* $crm_month_price = ($list[$k]['money']-$list[$k]['crm_oeseng']) / $list[$k]['crm_grvgkz'];
                $list[$k]['crm_month_price'] = round($crm_month_price, 2);*/
                $crm_month_price = bcdiv(($list[$k]['money']-$list[$k]['crm_oeseng']-$list[$k]['cost_production'])  , $list[$k]['crm_grvgkz'],2);
                $list[$k]['crm_month_price'] = $crm_month_price;
            }
            //核定成本
            $list[$k]['crm_oeseng'] = Receivables::where(["contract_id"=>$v["contract_id"],"check_status"=>2])->sum("crm_oeseng");
        }
        //新增 统计合同金额和回款金额 start
        $sumMoney = db('crm_contract')
            ->alias('contract')
            ->join('__CRM_CUSTOMER__ customer', 'contract.customer_id = customer.customer_id', 'LEFT')
            //->join('__CRM_BUSINESS__ business', 'contract.business_id = business.business_id', 'LEFT')
            ->join('__CRM_CONTACTS__ contacts', 'contract.contacts_id = contacts.contacts_id', 'LEFT')
            ->join('__CRM_RECEIVABLES_PLAN__ plan', 'contract.contract_id = plan.contract_id', 'LEFT')
            ->where($map)
            ->where($partMap)
            ->where($where)
//            ->where($authMap)
            ->field("sum(contract.money) money,GROUP_CONCAT(DISTINCT contract.contract_id) contract_id")
            ->order($order)
//            ->group('contract.contract_id')
            ->find();
        if (!empty($sumMoney)) {
            /*$doneMoney = 0;
            if (!empty($sumMoney["contract_id"])) {
                $doneMoney = $receivablesModel->where(['check_status' => 2])->whereIn("contract_id", $sumMoney["contract_id"])->sum('money');
            }
            $sumMoney = $sumMoney['money'];
            //$unMoney = $sumMoney-$doneMoney;
            $unMoney = $doneMoney;
            $unReceivablesMoney = $unMoney ?: '0.00';*/
            $doneMoney = 0;
            if (!empty($sumMoney["contract_id"])) {
                $doneMoney = $receivablesModel->where(['check_status' => 2,"crm_lasahm"=>"正常核算"])->whereIn("contract_id", $sumMoney["contract_id"])->sum('money');
            }
            $sumMoney = $sumMoney['money'];
            //$unMoney = $sumMoney-$doneMoney;
            $unMoney = $doneMoney;
            $unReceivablesMoney = $unMoney ?: '0.00';
        }
        //新增 统计合同金额和回款金额 end
        $data = [];
        $data['list'] = $list;
        $data['dataCount'] = $dataCount ?: 0;
        $data['data']['sumMoney'] = $sumMoney ?: 0.00;
        $data['data']['unReceivablesMoney'] = $unReceivablesMoney ?: 0.00;
        return $data;
    }
    /**
     * @param $user_id
     * 获取用户的所有下级
     */
    public function getChildIds($user_id){
        $childsArr=db("admin_user")->where(["parent_id"=>$user_id])->column("id");
        if($childsArr){
            foreach ($childsArr as $v){
                $arr=$this->getChildIds($v);
                if($arr) $childsArr=array_merge($childsArr,$arr);
            }
        }
        return $childsArr;
    }
    //根据IDs获取数组
    public function getDataByStr($idstr)
    {
        $idArr = stringToArray($idstr);
        if (!$idArr) {
            return [];
        }
        $list = Db::name('CrmContract')->where(['contract_id' => ['in', $idArr]])->select();
        return $list;
    }

    /**
     * 创建合同信息
     * @param
     * @return
     * @author Michael_xu
     */
    public function createData($param)
    {

        $fieldModel = new \app\admin\model\Field();
        $userModel = new \app\admin\model\User();
        $productModel = new \app\crm\model\Product();
        $confirmationModel = new \app\crm\model\ContractConfirmation();
        // 自动验证
        $validateArr = $fieldModel->validateField($this->name); //获取自定义字段验证规则
        $validate = new Validate($validateArr['rule'], $validateArr['message']);

        $result = $validate->check($param);
        if (!$result) {
            $this->error = $validate->getError();
            return false;
        }


        //处理部门、员工、附件、多选类型字段
        $arrFieldAtt = $fieldModel->getArrayField('crm_contract');
        foreach ($arrFieldAtt as $k => $v) {
            $param[$v] = arrayToString($param[$v]);
        }

        if ($param["money"] != $param["total_price"]) {
            $this->error = "合同金额与产品金额不一致";
            return false;
        }
        //新增 end
        //2023-10-26 lina 新增 start
        $param["arrears"] = $param["total_price"];//回款金额
        //2023-10-26 lina 新增 end
        if ($this->data($param)->allowField(true)->save()) {
            if ($param['product']) {
                //产品数据处理
                $resProduct = $productModel->createObject('crm_contract', $param, $this->contract_id);
                if ($resProduct == false) {
                    $this->error = '产品添加失败';
                    return false;
                }
            }
            //站内信
            $createUserInfo = $userModel->getDataById($param['create_user_id']);
            $send_user_id = stringToArray($param['check_user_id']);
            $sendContent = $createUserInfo['realname'] . '提交了合同【' . $param['name'] . '】,需要您审批';
            if ($send_user_id) {
                sendMessage($send_user_id, $sendContent, $this->contract_id, 1);
            }

            $data = [];
            $data['contract_id'] = $this->contract_id;

            //2023-12-20 lina 新增 start
            //新增回款记录
            Receivables::create([
                "number" => $param["number"],
                "customer_id" => $param["customer_id"],
                "contract_id" => $this->contract_id,
                "return_time" => $param["return_time"],
                "return_type" => $param["return_type"],
                "money" => $param["receivables_money"],
                "remark" => $param["receivables_remark"],
                "crm_lasahm" => "正常核算",
                "flow_id" => $param["flow_id"],
                "order_id" => $param["order_id"],
                "check_user_id" => $param["check_user_id"],
                "flow_user_id" => isset($param["flow_user_id"]) ? $param["flow_user_id"] : 0,
                /*"flow_id"=>0,
                "order_id"=>0,
                "check_user_id"=>0,
                "flow_user_id"=>0,*/
                "create_user_id" => $param['create_user_id'],
                "owner_user_id" => $param['create_user_id'],
                "create_time" => time(),
                "crm_quhkfe" => $param['crm_quhkfe'],
                "is_start" => 1,
                "crm_oeseng" => $param["crm_oeseng"],//核定成本
                "crm_ahokzi"=>$param["crm_ahokzi"],//单量
            ]);
            //2023-12-20 lina 新增 end
            return $data;
        } else {
            $this->error = '添加失败';
            return false;
        }
    }

    /**
     * 编辑合同主表信息
     * @param
     * @return
     * @author Michael_xu
     */
    public function updateDataById($param, $contract_id = '')
    {
        $productModel = new \app\crm\model\Product();
        $userModel = new \app\admin\model\User();
        $dataInfo = db('crm_contract')->where(['contract_id' => $contract_id])->find();
        //新增 已完结合同不能修改 start
        $res = $this->getOkStatus($dataInfo["contract_id"]);
        if ($res != true) {
            $this->error = '该状态不允许修改';
            return false;
        }
        $create_user_id = $param['create_user_id'];
        //新增 已完结合同不能修改 end
        //过滤不能修改的字段
        $unUpdateField = ['create_user_id', 'is_deleted', 'delete_time'];
        foreach ($unUpdateField as $v) {
            unset($param[$v]);
        }
        $param['contract_id'] = $contract_id;
        $fieldModel = new \app\admin\model\Field();
        // 自动验证
        $validateArr = $fieldModel->validateField($this->name); //获取自定义字段验证规则
        $validate = new Validate($validateArr['rule'], $validateArr['message']);

        $result = $validate->check($param);
        if (!$result) {
            $this->error = $validate->getError();
            return false;
        }

        //处理部门、员工、附件、多选类型字段
        $arrFieldAtt = $fieldModel->getArrayField('crm_contract');
        foreach ($arrFieldAtt as $k => $v) {
            $param[$v] = arrayToString($param[$v]);
        }

        //2023-10-26 lina 新增 start
        $param["arrears"] = $param["total_price"];//回款金额
        //2023-10-26 lina 新增 end
        if ($this->allowField(true)->save($param, ['contract_id' => $contract_id])) {
            //产品数据处理
            $resProduct = $productModel->createObject('crm_contract', $param, $contract_id);
            //修改记录
            $dataInfo["check_status"]=$this->statusArr[$dataInfo["check_status"]];
            $param["check_status"]=$this->statusArr[$param["check_status"]];
            updateActionLog($param['user_id'], 'crm_contract', $contract_id, $dataInfo, $param);
            //站内信
            $createUserInfo = $userModel->getDataById($param['user_id']);
            $send_user_id = stringToArray($param['check_user_id']);
            $sendContent = $createUserInfo['realname'] . '提交了合同【' . $param['name'] . '】,需要您审批';
            if ($send_user_id) {
                sendMessage($send_user_id, $sendContent, $contract_id, 1);
            }
            $data = [];
            $data['contract_id'] = $contract_id;

            //2023-12-20 lina 新增 start
            //新增回款记录
            Receivables::where([
                'is_start'=>1,
                'contract_id'=>$this->contract_id
            ])->update([
                "customer_id" => $param["customer_id"],
                "contract_id" => $this->contract_id,
                "return_time" => $param["return_time"],
                "return_type" => $param["return_type"],
                "money" => $param["receivables_money"],
                "remark" => $param["receivables_remark"],
                "crm_lasahm" => $param["crm_lasahm"],
                "flow_id" => $param["flow_id"],
                "order_id" => $param["order_id"],
                "check_user_id" => $param["check_user_id"],
                "flow_user_id" => isset($param["flow_user_id"]) ? $param["flow_user_id"] : 0,
                /*"flow_id"=>0,
                "order_id"=>0,
                "check_user_id"=>0,
                "flow_user_id"=>0,*/
                "create_user_id" => $create_user_id,
                "owner_user_id" => $create_user_id,
                "create_time" => time(),
                "crm_quhkfe" => $param['crm_quhkfe'],
                "is_start" => 1,
                "check_status" => 0,
                "crm_oeseng" => $param["crm_oeseng"],//核定成本
                "crm_ahokzi"=>$param["crm_ahokzi"],//单量
            ]);


            // //将审批记录至为无效
            // $examineRecordModel = new \app\admin\model\ExamineRecord();
            // $examineRecordModel->setEnd(['types' => 'crm_contract', 'types_id' => $param['id']]);

            return $data;
        } else {
            $this->error = '编辑失败';
            return false;
        }
    }

    /**
     * 合同数据
     * @param  $id 合同ID
     * @return
     */
    public function getDataById($id = '')
    {
        $receivablesModel = new \app\crm\model\Receivables();
        $userModel = new \app\admin\model\User();
        $map['contract_id'] = $id;
        $dataInfo = $this->where($map)->find();
        if (!$dataInfo) {
            $this->error = '暂无此数据';
            return false;
        }
        $dataInfo['create_user_info'] = isset($dataInfo['create_user_id']) ? $userModel->getUserById($dataInfo['create_user_id']) : [];
        $dataInfo['owner_user_id_info'] = isset($dataInfo['owner_user_id']) ? $userModel->getUserById($dataInfo['owner_user_id']) : [];
        //$dataInfo['business_id_info'] = $dataInfo['business_id'] ? db('crm_business')->where(['business_id' => $dataInfo['business_id']])->field('business_id,name')->find() : [];
        $dataInfo['customer_id_info'] = $dataInfo['customer_id'] ? db('crm_customer')->where(['customer_id' => $dataInfo['customer_id']])->field('customer_id,name')->find() : [];
        //回款金额
        $receivablesMoney = $receivablesModel->getMoneyByContractId($id);
        $dataInfo['receivablesMoney'] = $receivablesMoney ?: [];
        //新增 统计成本变更后的金额 start
        $add_money = Examine::alias('examine')
            ->join('__OA_EXAMINE_RELATION__ examine_relation', 'examine_relation.examine_id = examine.examine_id', 'LEFT')
            ->where(["examine.category_id" => 15, "examine.check_status" => 2, "crm_porzod" => "增加"])
            ->where("examine_relation.contract_ids like '%," . $id . ",%'")
            ->sum("money");
        $reduce_money = Examine::alias('examine')
            ->join('__OA_EXAMINE_RELATION__ examine_relation', 'examine_relation.examine_id = examine.examine_id', 'LEFT')
            ->where("examine_relation.contract_ids like '%," . $id . ",%'")
            ->where(["examine.category_id" => 15, "examine.check_status" => 2, "crm_porzod" => "减少"])->sum("money");
        $money = bcsub($add_money, $reduce_money, 2);

        //$dataInfo["crm_cost_price"]=bcadd($dataInfo["crm_xuonxx"],$money)?:$dataInfo["crm_xuonxx"];//变更后成本价
        //新增 统计成本变更后的金额 end
        //新增 统计数据 start
        $dataInfo["arrears"]=bcsub($dataInfo["total_price"],$receivablesMoney["lasahmMoney"],2);//尾款金额 产品总金额-回款金额
//        $dataInfo["arrears"]=$dataInfo["money"]-$receivablesMoney["contractMoney"];//尾款金额 合同金额-回款金额

        $dataInfo["crm_pbeelz"] = bcadd(bcadd($dataInfo["crm_gvzubs"], $dataInfo["crm_vwvucv"], 2), $dataInfo["crm_ahsoqz"], 2);//核算成本
        //$dataInfo["crm_pbeelz"] = bcadd($dataInfo["crm_pbeelz"], $dataInfo["cost_production"], 2);//+工本费

        //if($dataInfo["crm_pbeelz"]<0) $dataInfo["crm_pbeelz"]=abs($dataInfo["crm_pbeelz"]);
        if ($dataInfo["crm_pbeelz"] < 0) {
            //$dataInfo["crm_pbeelz"]=abs($dataInfo["crm_pbeelz"]);
            $dataInfo["performance"] = bcsub($receivablesMoney["lasahmMoney"], abs($receivablesMoney["hsOeseng"]), 2)?:0;//业绩 回款金额-核定成本
        } else {
            $dataInfo["performance"] = bcsub($receivablesMoney["lasahmMoney"], $receivablesMoney["hsOeseng"], 2)?:0;//业绩 回款金额-核定成本

        }
        //统计核定成本
        $dataInfo["crm_oeseng"] = $receivablesMoney["hsOeseng"];
        //新增 统计数据 end
        return $dataInfo;
    }

    /**
     * [合同转移]
     * @param ids 合同ID数组
     * @param owner_user_id 变更负责人
     * @param is_remove 1移出，2转为团队成员
     * @return
     * @author Michael_xu
     */
    public function transferDataById($ids, $owner_user_id, $type = 1, $is_remove)
    {
        $settingModel = new \app\crm\model\Setting();
        $errorMessage = [];
        foreach ($ids as $id) {
            $contractInfo = [];
            $contractInfo = db('crm_contract')->where(['contract_id' => $id])->find();
            if (in_array($contractInfo['check_status'], ['0', '1'])) {
                $errorMessage[] = '合同：' . $contractInfo['name'] . '"转移失败，错误原因：审批中，无法转移；';
                continue;
            }

            $data = [];
            $data['owner_user_id'] = $owner_user_id;
            $data['update_time'] = time();
            if (!db('crm_contract')->where(['contract_id' => $id])->update($data)) {
                $errorMessage[] = '合同：' . $contractInfo['name'] . '"转移失败，错误原因：数据出错；';
                continue;
            }
            //团队成员
            $teamData = [];
            $teamData['type'] = $type; //权限 1只读2读写
            $teamData['user_id'] = [$contractInfo['owner_user_id']]; //协作人
            $teamData['types'] = 'crm_contract'; //类型
            $teamData['types_id'] = $id; //类型ID
            $teamData['is_del'] = ($is_remove == 1) ? 1 : '';
            $res = $settingModel->createTeamData($teamData);
        }
        if ($errorMessage) {
            return $errorMessage;
        } else {
            return true;
        }
    }

    /**
     * 根据对象ID 获取该年各个月合同金额
     * @return [year] [哪一年]
     * @return [owner_user_id] [哪个员工]
     * @return [start_time] [开始时间]
     * @return [end_time] [结束时间]
     */
    public function getDataByUserId($param)
    {
        if ($param['obj_type']) {
            if ($param['obj_type'] == 1) { //部门
                $userModel = new \app\admin\model\User();
                $str = $userModel->getSubUserByStr($param['obj_id'], 1) ?: ['-1'];
                $map['owner_user_id'] = array('in', $str);
            } else { //员工
                $map['owner_user_id'] = $param['obj_id'];
            }
        }
        //审核状态
        $start = date('Y-m-d', $param['start_time']);
        $stop = date('Y-m-d', $param['end_time']);
        $map['check_status'] = 2;
        $data = $this->where($map)->where(['order_date' => ['between', [$start, $stop]]])->sum('money');
        return $data;
    }

    /**
     * 获取订单是否完结
     * */
    public function getOkStatus($contract_id)
    {
        $contract = $this->field("is_ok")->where(["contract_id" => $contract_id])->find();
        if (empty($contract) || $contract["is_ok"] == 1) return false;
        return true;
    }



    //2023-12-19 lina 新增 start

    /**
     * [getDataList 记账板块list]
     * @param     [string]                   $map [查询条件]
     * @param     [number]                   $page     [当前页数]
     * @param     [number]                   $limit    [每页数量]
     * @return    [array]                    [description]
     * @author Michael_xu
     */
    public function getAccountingList($request, $type = 0)
    {
        $userModel = new \app\admin\model\User();
        $structureModel = new \app\admin\model\Structure();
        $fieldModel = new \app\admin\model\Field();
        $receivablesModel = new \app\crm\model\Receivables();
        $search = $request['search'];
        $user_id = $request['user_id'];
        $scene_id = (int)$request['scene_id'];
        unset($request['scene_id']);
        unset($request['search']);
        unset($request['user_id']);
        //修改 start
        $where = [];
        if (isset($request["is_ok"])) {
            $where["is_ok"] = $request["is_ok"];
            unset($request['is_ok']);
        }
        if (isset($request["check_status"])) {
            $where["check_status"] = $request["check_status"];
            unset($request['check_status']);
        }
        /*if(isset($request["owner_user_id"])){
            //判断是否是管家身份
            $userInfo=$userModel->where(["id"=>$request["owner_user_id"]])->find();
            //判断是不是外勤和会计
            $idArr=db('crm_contract')->where('ro_user_id',array('like','%,'.$userInfo['id'].',%'))
                ->whereOr('rw_user_id',array('like','%,'.$userInfo['id'].',%'))->field("contract_id")->select();
            if($userInfo["structure_id"]==6){
                $where["contract.butler_id"]=["in",[0,$userInfo["id"]]];
            }
            unset($request['owner_user_id']);
        }*/
        $authIds = $userModel->getUserByPer('crm', 'contract', 'index');

        //2023-12-19 lina 新增 start
        //如果是记账会计或者咨询会计 只查自己的工单
        $group = db('admin_access')->where(['user_id' => $user_id])->whereIn("group_id", [35, 36])->find();
        $accountancy_user_where = "";
        if (!empty($group)) {
            $accountancy_user_where = "contract.accountancy_user_id=" . $user_id . " or contract.consult_user_id=" . $user_id;
        }
        //如果是会计经理 只查自己捡取的工单和待捡取工单
        $group = db('admin_access')->where(['user_id' => $user_id])->where(["group_id" => 33])->find();
        if (!empty($group)) {
            $accountancy_user_where = "contract.accounting_id=" . $user_id . " or contract.accounting_status=0";
        }
        //2023-12-19 lina 新增 start
        //修改 end
        $request = $this->fmtRequest($request);
        $requestMap = $request['map'] ?: [];
        $sceneModel = new \app\admin\model\Scene();
        if ($scene_id) {
            //自定义场景
            $sceneMap = $sceneModel->getDataById($scene_id, $user_id, 'contract') ?: [];
        } else {
            //默认场景
            $sceneMap = $sceneModel->getDefaultData('contract', $user_id) ?: [];
        }
        if ($search) {
            //普通筛选
            $sceneMap['name'] = ['condition' => 'contains', 'value' => $search, 'form_type' => 'text', 'name' => '合同名称'];
        }
        $partMap = [];
        //优先级：普通筛选>高级筛选>场景
        if ($sceneMap['contract.ro_user_id'] && $sceneMap['contract.rw_user_id']) {
            //相关团队查询
            $map = $requestMap;
            $partMap = function ($query) use ($sceneMap) {
                $query->where('contract.ro_user_id', array('like', '%,' . $sceneMap['ro_user_id'] . ',%'))
                    ->whereOr('contract.rw_user_id', array('like', '%,' . $sceneMap['rw_user_id'] . ',%'));
            };
        } else {
            $map = $requestMap ? array_merge($sceneMap, $requestMap) : $sceneMap;
        }
        //高级筛选
        $map = where_arr($map, 'crm', 'contract', 'index');
        $order = ['contract.create_time desc'];
        $authMap = [];
        if (!$partMap) {
            //修改 start
            if (empty($type)) {
                $auth_user_ids = $userModel->getUserByPer('crm', 'contract', 'index');
            }
            if ($type == 1) {
                $auth_user_ids = $userModel->getUserByPer('crm', 'contract', 'workindex');
            }
            if ($type == 2) {
                $auth_user_ids = $userModel->getUserByPer('crm', 'contract', 'okindex');
            }
            //修改 end
            if (isset($map['contract.owner_user_id'])) {
                if (!is_array($map['contract.owner_user_id'][1])) {
                    $map['contract.owner_user_id'][1] = [$map['contract.owner_user_id'][1]];
                }
                if ($map['contract.owner_user_id'][0] == 'neq') {
                    $auth_user_ids = array_diff($auth_user_ids, $map['contract.owner_user_id'][1]) ?: [];    //取差集
                } else {
                    $auth_user_ids = array_intersect($map['contract.owner_user_id'][1], $auth_user_ids) ?: [];    //取交集
                }
                unset($map['contract.owner_user_id']);
                $auth_user_ids = array_merge(array_unique(array_filter($auth_user_ids))) ?: ['-1'];
                $authMap['contract.owner_user_id'] = array('in', $auth_user_ids);
            } else {
                $authMapData = [];
                $authMapData['auth_user_ids'] = $auth_user_ids;
                $authMapData['user_id'] = $user_id;
                if (empty($type)) {
                    $authMap = function ($query) use ($authMapData) {
                        $query->where('contract.owner_user_id', array('in', $authMapData['auth_user_ids']))
                            ->whereOr('contract.ro_user_id', array('like', '%,' . $authMapData['user_id'] . ',%'))
                            ->whereOr('contract.rw_user_id', array('like', '%,' . $authMapData['user_id'] . ',%'));
                    };
                }
            }
        }
        //合同签约人
        if ($map['contract.order_user_id']) {
            $map['contract.order_user_id'] = ['like', '%,' . $map['contract.order_user_id'][1] . ',%'];
        }
        //列表展示字段
        // $indexField = $fieldModel->getIndexField('crm_contract', $user_id);
        //人员类型
        $userField = $fieldModel->getFieldByFormType('crm_contract', 'user');
        $structureField = $fieldModel->getFieldByFormType('crm_contract', 'structure');  //部门类型

        $readAuthIds = $userModel->getUserByPer('crm', 'contract', 'read');
        $updateAuthIds = $userModel->getUserByPer('crm', 'contract', 'update');
        $deleteAuthIds = $userModel->getUserByPer('crm', 'contract', 'delete');

        if(isset($map["contract.accountancy_user_realname"])){
            $map["accountancy_user.realname"]=$map["contract.accountancy_user_realname"];
            unset($map["contract.accountancy_user_realname"]);
        }
        if(isset($map["contract.consult_user_realname"])){
            $map["consult_user.realname"]=$map["contract.consult_user_realname"];
            unset($map["contract.consult_user_realname"]);
        }
        /*dump($map);
        dump($partMap);
        dump($where);
        dump($accountancy_user_where);
        die();*/
        $list = db('crm_contract')
            ->alias('contract')
            ->join('__CRM_CUSTOMER__ customer', 'contract.customer_id = customer.customer_id', 'LEFT')
            //->join('__CRM_BUSINESS__ business', 'contract.business_id = business.business_id', 'LEFT')
            ->join('__CRM_CONTACTS__ contacts', 'contract.contacts_id = contacts.contacts_id', 'LEFT')
            ->join('__CRM_RECEIVABLES_PLAN__ plan', 'contract.contract_id = plan.contract_id', 'LEFT')
            ->join('__ADMIN_USER__ accounting_user', 'contract.accounting_id = accounting_user.id', 'LEFT')//会计经理
            ->join('__ADMIN_USER__ accountancy_user', 'contract.accountancy_user_id = accountancy_user.id', 'LEFT')//做账会计
            ->join('__ADMIN_USER__ consult_user', 'contract.consult_user_id = consult_user.id', 'LEFT')//咨询会计
            ->join('__ADMIN_USER__ butler', 'contract.butler_id = butler.id', 'LEFT')//管家
            ->where($map)
            ->where($partMap)
            ->where($where)
            ->where($accountancy_user_where)
//              ->where($authMap)
            ->limit(($request['page'] - 1) * $request['limit'], $request['limit'])//,business.name as business_name
            ->field('contract.*,customer.name as customer_name,contacts.name as contacts_name,accounting_user.realname as accounting_user_realname,
            accountancy_user.realname as accountancy_user_realname,consult_user.realname as consult_user_realname,butler.realname butler_realname')
            // ->field('contract_id,'.implode(',',$indexField))
            ->order($order)
            ->group('contract.contract_id')//->fetchsql()
            ->select();

        /*dump($list);
        die();*/
        $dataCount = db('crm_contract')
            ->alias('contract')
            ->join('__CRM_CUSTOMER__ customer', 'contract.customer_id = customer.customer_id', 'LEFT')
            //->join('__CRM_BUSINESS__ business', 'contract.business_id = business.business_id', 'LEFT')
            ->join('__CRM_CONTACTS__ contacts', 'contract.contacts_id = contacts.contacts_id', 'LEFT')
            ->join('__CRM_RECEIVABLES_PLAN__ plan', 'contract.contract_id = plan.contract_id', 'LEFT')
            ->join('__ADMIN_USER__ accountancy_user', 'contract.accountancy_user_id = accountancy_user.id', 'LEFT')//做账会计
            ->join('__ADMIN_USER__ consult_user', 'contract.consult_user_id = consult_user.id', 'LEFT')//咨询会计
            ->join('__ADMIN_USER__ butler', 'contract.butler_id = butler.id', 'LEFT')//管家
            ->where($where)
            ->where($map)
            ->where($partMap)
            ->where($accountancy_user_where)
//                    ->where($authMap)
            ->group('contract.contract_id')->count('contract.contract_id');
        $accountingModel = model('Accounting');
        $ids=[];
        $all_monthly_agency_price=0;
        $monthly_agency_price=0;
        foreach ($list as $k => $v) {
            $ids[]=$v["contract_id"];
            $list[$k]['create_user_id_info'] = isset($v['create_user_id']) ? $userModel->getUserById($v['create_user_id']) : [];
            $list[$k]['owner_user_id_info'] = isset($v['owner_user_id']) ? $userModel->getUserById($v['owner_user_id']) : [];
            foreach ($userField as $key => $val) {
                $list[$k][$val . '_info'] = isset($v[$val]) ? $userModel->getListByStr($v[$val]) : [];
            }
            foreach ($structureField as $key => $val) {
                $list[$k][$val . '_info'] = isset($v[$val]) ? $structureModel->getDataByStr($v[$val]) : [];
            }
            //2023-12-19 修改 start
            $list[$k]['accountancy_user_info']['id'] = $v['accountancy_user_id'];
            $list[$k]['accountancy_user_info']['realname'] = $v['accountancy_user_realname'];
            $list[$k]['consult_user_info']['id'] = $v['consult_user_id'];
            $list[$k]['consult_user_info']['realname'] = $v['consult_user_realname'];
            //2023-12-19 修改 end
           /* $list[$k]['business_id_info']['business_id'] = $v['business_id'];
            $list[$k]['business_id_info']['name'] = $v['business_name'];*/
            $list[$k]['customer_id_info']['customer_id'] = $v['customer_id'];
            $list[$k]['customer_id_info']['name'] = $v['customer_name'];
            $list[$k]['contacts_id_info']['contacts_id'] = $v['contacts_id'];
            $list[$k]['contacts_id_info']['name'] = $v['contacts_name'];
            $moneyInfo = [];
            $moneyInfo = $receivablesModel->getMoneyByContractId($v['contract_id']);
            //$list[$k]['unMoney'] = $moneyInfo['unMoney'] ? : 0.00;//未回款金额
            //$list[$k]['unMoney'] = $moneyInfo['doneMoney'] ?: 0.00;//已回款金额--修改
            $list[$k]['doneMoney'] = $moneyInfo['lasahmMoney'] ?: 0.00;//已回款金额--修改
            $list[$k]['check_status_info'] = $this->statusArr[$v['check_status']];
            //修改 lina start
            $list[$k]['state_info'] = $this->stateArr[$v['state']];
            if ($v["is_ok"] == 1) {
                $list[$k]['check_status_info'] = "已完结";
                $list[$k]['state_info'] = "已完结";
            }
            if ($v["check_status"] != 2) {
                $list[$k]['state_info'] = $list[$k]['check_status_info'];
            }
            //修改 lina end
            $planInfo = [];
            $planInfo = db('crm_receivables_plan')->where(['contract_id' => $v['contract_id']])->find();
            $list[$k]['receivables_id'] = $planInfo['receivables_id'] ?: '';
            $list[$k]['remind_date'] = $planInfo['remind_date'] ?: '';
            $list[$k]['return_date'] = $planInfo['return_date'] ?: '';
            //权限
            $roPre = $userModel->rwPre($user_id, $v['ro_user_id'], $v['rw_user_id'], 'read');
            $rwPre = $userModel->rwPre($user_id, $v['ro_user_id'], $v['rw_user_id'], 'update');
            $permission = [];
            $is_read = 0;
            $is_update = 0;
            $is_delete = 0;
            if (in_array($v['owner_user_id'], $readAuthIds) || $roPre || $rwPre) $is_read = 1;
            if (in_array($v['owner_user_id'], $updateAuthIds) || $rwPre) $is_update = 1;
            if (in_array($v['owner_user_id'], $deleteAuthIds)) $is_delete = 1;
            $permission['is_read'] = $is_read;
            $permission['is_update'] = $is_update;
            $permission['is_delete'] = $is_delete;
            $list[$k]['permission'] = $permission;
            $receivablesMoney = $receivablesModel->getMoneyByContractId($v['contract_id']);
            $list[$k]['crm_oeseng']=$receivablesMoney["hsOeseng"];
            //月度服务费
            $list[$k]['crm_month_price'] = $v["monthly_agency_price"];
            if (!empty($list[$k]['crm_grvgkz'])) {
                //月度服务费=(订单金额-核定成本-工本费)/期数
                //核定成本汇总
                $crm_oeseng=db("crm_receivables")->where(["contract_id"=>$v["contract_id"],"crm_lasahm"=>"正常核算","check_status"=>2])->sum("crm_oeseng");
                $list[$k]['crm_month_price'] = bcdiv(($v['money']-$crm_oeseng-$v['cost_production'])  , $v['crm_grvgkz'],2);
                $list[$k]["monthly_agency_price"]=$list[$k]['crm_month_price'];
            }
            //新增 记账状态 -- start
            $list[$k]['accounting_status'] = "进行中";
            $accounting = $accountingModel->where(["status" => 0, "contract_id" => $v["contract_id"]])->find();
            if (!empty($accounting)) {
                $list[$k]['accounting_status'] = "待审核记账";
            }
            //5.记账利润计算公式=服务期数*月代理费单价+工本费；
            //已服务期数
            $account = $accountingModel->where(["status" => 1, "contract_id" => $v["contract_id"]])->count();
            $accounting_profit = bcmul($account, $v["monthly_agency_price"]);//记账利润；
            $accounting_profit = bcadd($accounting_profit, $v["cost_production"]);
            $list[$k]["accounting_profit"] = $accounting_profit;
            //新增 记账状态 -- end
            $account_num=Accounting::where(["contract_id"=>$v["contract_id"],"status"=>1])->count();
            $num=$v["available"]-$account_num?:0;
            $list[$k]["crm_grvgkz"]=$num.'/'.$v["crm_grvgkz"];
            $price=bcmul($account_num,$v["monthly_agency_price"],2);//已做账次数*月度服务费单价
            $all_monthly_agency_price+=$price;
            $monthly_agency_price+=$v["monthly_agency_price"];
            //已做账期数
            $list[$k]["bookkept"]=$account_num;
        }
        $data = [];
        $data['list'] = $list;
        $data['dataCount'] = $dataCount ?: 0;
        $data['data']['monthly_agency_price'] = $monthly_agency_price ?: 0.00;
        $data['data']['all_monthly_agency_price'] = $all_monthly_agency_price ?: 0.00;
        return $data;
    }

    /**
     * 获取会计列表
     * */
    public function accountancyUserList($type)
    {
        $where = ["group_id" => 35];
        if ($type == 2) {
            //咨询会计
            $where = ["group_id" => 36];
        }
        $group = db('admin_access')->where($where)->column("user_id");
        $user = User::whereIn("id", $group)->select();
        return $user;
    }
    //lina 新增 end
}